let egg = require('egg');
let v4 = require('uuid');
const axios = require("axios");
const uuidv4 = v4.v4;
const Service = egg.Service;
class BaseService extends Service {
  _headers(opts) {
    return {
      Cookie: 'Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1623339177,1623339183; _ga=GA1.2.1195980605.1579367081; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1623339982; kw_token=3E7JFQ7MRPL; _gid=GA1.2.747985028.1623339179; _gat=1',
      csrf: '3E7JFQ7MRPL',
      Host: 'www.kuwo.cn',
      Referer: 'http://www.kuwo.cn/',
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36',
      ...opts,
    }
  }

  async commonRequest(url, options) {
    //console.log(222)
    const reqId = uuidv4()
    const opts = {
      method: 'GET',
      dataType: 'json',
      timeout: 10000,
      ...options,
      headers: this._headers(options?.headers),
      url: `${url}&reqId=${reqId}`,
    }
    //console.log(opts)
    return new Promise(resolve => {
      axios({...opts}).then((r) => {
          //console.log('then',r)
          resolve(r.data)
      }).catch((r) => {
        //console.log('catch',r)
          resolve(r.data)
      })
    })


  }
}

module.exports = BaseService
